<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="3">
        <el-select
          v-model="search.filter_project_id"
          filterable
          clearable
          placeholder="请选择项目"
        >
          <el-option
            v-for="item in search.project_options"
            :key="item.id"
            :label="item.project_name"
            :value="item.id"
          />
        </el-select>
      </el-col>
      <el-col :span="6">
        <el-input
          v-model="search.config_name"
          placeholder="请输入配置名称"
          prefix-icon="el-icon-search"
          @keyup.enter.native="SearchConfig()"
        />
      </el-col>
      <el-col :span="3">
        <el-button
          type="primary"
          icon="el-icon-search"
          round
          @click="SearchConfig()"
        >搜索
        </el-button>
      </el-col>
      <el-col :span="10">
        <el-button
          type="success"
          style="float:right"
          @click="skipImportConfig"
        >导入配置
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button
          type="success"
          style="float:right"
          @click="skipCreateConfig"
        >新增配置
        </el-button>
      </el-col>
    </el-row>
    <br>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" min-width="5%">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="配置名称" min-width="20%">
        <template slot-scope="scope">
          <el-link
            style="color:#298abd"
            @click="edit(scope.row.id)"
          >{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" min-width="10%" align="center">
        <template slot-scope="scope">
          {{ scope.row.belong_project_name }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="更新时间" min-width="12%">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ $common.formatDateTime(scope.row.update_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="编写人员" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.author }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="12%" align="center">
        <template slot-scope="scope">
          <el-button-group>
            <el-tooltip content="编辑" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="edit(scope.row.id)"
              />
            </el-tooltip>
            <el-tooltip content="复制" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="warning"
                icon="el-icon-copy-document"
                @click="copy(scope.row.id)"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="bottom" effect="light">
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="del(scope.row.id)"
              />
            </el-tooltip>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="pagination.page_num"
      :page-sizes="[10, 20]"
      :page-size="pagination.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pagination.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getProjectSumList } from '@/api/project/table'
import { getConfigList, deleteConfig } from '@/api/config/table'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: {
        project_options: [],
        filter_project_id: null,
        config_name: ''
      },
      pagination: {
        total: 0,
        page_current: 1,
        page_num: 1,
        page_size: 10
      },
      list: null,
      listLoading: true,
      ispagination: false
    }
  },
  created() {
    this.fetchProjectData()
    this.fetchConfigData()
  },
  methods: {
    skipImportConfig() {
      this.$router.push({
        path: '/config/import_config'
      })
    },
    skipCreateConfig() {
      this.$router.push({
        path: '/config/create_config'
      })
    },
    fetchProjectData() {
      getProjectSumList().then(response => {
        this.search.project_options = response.results
      })
    },
    fetchConfigData() {
      this.listLoading = true
      var params = {}
      params['ordering'] = '-update_time'
      getConfigList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
    },
    SearchConfig: function() {
      var params = {}
      if (this.search.filter_project_id) {
        params['belong_project'] = this.search.filter_project_id
      }

      if (this.search.config_name) {
        params['search'] = this.search.config_name
      }

      if (this.ispagination) {
        params['page_num'] = this.pagination.page_num
        params['page_size'] = this.pagination.page_size
      } else {
        params['page_num'] = 1
        params['page_size'] = this.pagination.page_size
      }
      params['ordering'] = '-update_time'

      this.listLoading = true
      getConfigList(params).then(response => {
        this.list = response.results
        this.listLoading = false
        this.pagination.total = response.count
      })
      if (this.ispagination === false) {
        this.pagination.page_num = 1
      }
      this.ispagination = false
    },

    handleSizeChange: function(val) {
      this.pagination.page_size = val
      this.SearchConfig()
    },
    handleCurrentChange: function(val) {
      this.ispagination = true
      this.pagination.page_num = val
      this.SearchConfig()
    },

    edit(arg) {
      this.$router.push({
        path: '/config/update_config',
        query: {
          id: arg,
          operation_type: 1
        }
      })
    },
    copy(arg) {
      this.$router.push({
        path: '/config/create_config',
        query: {
          id: arg,
          operation_type: 2
          // eslint-disable-next-line no-mixed-spaces-and-tabs
			  }
        // eslint-disable-next-line no-mixed-spaces-and-tabs
		  })
    },
    del(arg) {
      this.$confirm('此操作将删除配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {}
        params['id'] = arg
        deleteConfig(params).then(response => {
          if (response.status === '000000') return
          this.SearchConfig()
        })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
